#!/bin/sh

# Try to find a non-crippled sed.
#
# Copyright (C) 2000-2006 Free Software Foundation, Inc.
#
# Author: Frank Heckenbach <frank@pascal.gnu.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Pascal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Pascal; see the file COPYING. If not, write to the
# Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

simple=""
if [ x"$1" = x"-s" ]; then
  simple="yes"
  shift
fi

if [ $# -gt 0 ]; then
  echo "Usage: `basename "$0"` [-s]" >&2
  echo "  -s  Only do simpler checks" >&2
  exit 1
fi

if [ x"$TMPDIR" = x ]; then
  TMPDIR="/tmp"
fi
TMP="$TMPDIR/`basename "$0"`.$$"
rm -rf "$TMP" || exit 1
mkdir "$TMP" || exit 1
cd "$TMP" || exit 1
trap 'cd /; rm -rf "$TMP"; exit 1' INT 0

cat << EOF > tmp1 || exit 1
xy
xvy
xvvy
xvvvy
xxvvvy
xvvvvy
xvvyxvvvyxvvy
qh
we
hq
we
qwe
q
we
we
we
xx
---
hq
we
qwe
q
we
we
we
EOF

cat << EOF > tmp2 || exit 1
xy
xvy
-vv-xvvy-
r-
-vvv-xvvvy-
r-
x-vvv-xvvvy-
r-
xvvvvy
-vv-xvvy-
r--vvv-xvvvy-
r--vv-xvvy-
r-
qh
we
ha
ha
q
qwe
a
a
a
a
a
a
q
xx
EOF

cat << EOF > tmp4 || exit 1
q
x
ab ge
dx
Cd fef
x
ad gf
foo
h
jk
.
jqk
.
jqwek

.
asdjyxckvbn
 .
.
jqk
jqk
i
bar
EOF

cat << EOF > tmp5 || exit 1
-ab ge-ab-e-
d-Cd fef-Cd-fef-
h
jk
.

        :q:
.

        :qwe:
.
asd
        :yxc:vbn
 .
.

        :q:
jqk
i
EOF

# Search for sed by using sed ... ;-)
if [ x"$SED" = x ]; then SED=gsed; fi
for sed in $SED sed \
           `echo "$PATH:.:" | sed -e "s,::*,:,g;s,:,/sed ,g"` \
           `echo "$PATH:.:" | sed -e "s,::*,:,g;s,:,/$SED ,g"`; do

if { "$sed" -e '
  /^---$/,$d;
  s/x\(.\{2,3\}\)y/-\1-&-\
r-/g;
  : loop;
  /q/N;
  /q\nwe/{
    s//a\
q/;
    P;
    b loop;
  }
' < tmp1 > tmp3; } 2>/dev/null &&
cmp -s tmp2 tmp3 && {
[ x"$simple" != x ] || {
{ "$sed" -ne '
  /x/N;
  s/x.\(\([Aa][Bb]\|[Cc][Dd]\) \+g\?\([ef]\+\)\)/-\1-\2-\3-/p;
  /h/,/i/{
    /j\(.\+\)k/{
      s//\
        :\1:/;
      N;
      s/\n$//;
    };
    p;
  };
' < tmp4 > tmp6; } 2>/dev/null &&
cmp -s tmp5 tmp6;
}; }; then
  cd /
  rm -rf "$TMP" || exit 1
  trap "" 0
  echo "$sed"
  exit 0
fi

done

cd /
rm -rf "$TMP" || exit 1
trap "" 0

cat << EOF >&2

Sorry, I could not find a non-crippled version of \`sed' (one that
understands regular expressions, in particular \`\|'). If or when
you have installed one, e.g., GNU sed, available from
ftp://ftp.gnu.org/pub/gnu/sed/
please install it as \`sed', \`gsed', or point the environment
variable \`SED' to it, and re-run.

EOF

exit 1
